#pragma once
#include "framework.h"
#include "stdlib.h"
#include "Pool.h"

using namespace poly;

struct Player
{
	RigidBody body;
	poly::Polygon polygon;
	PolygonView view;
};

struct MeteorClass
{
	MeteorClass( D3DDevice device ) : count(0)
	{
		int vertices = 3 + rand()%1;
		for( int i = 0; i < vertices; i++ )
		{
			float r = (rand()%20+20)/50.f;
			polygon.push_back( float3( cos(2*PI/vertices*i)*r, sin(2*PI/vertices*i)*r, 0.f ) );
		}

		float3 center = polygon.center();
		for( int i = 0; i < polygon.size(); i++ )
			polygon[i] -= center;

		float density = (rand()%4+4)/6.f;

		invMass = 1/polygon.area()/density;
		invInertia = invMass * inverse(polygon.momentOfInertia());

		view.initialize( device );
		view.setup( polygon, float4(1.f, .5f, .5f, .8f) );
	}

	poly::Polygon polygon;
	PolygonView view;
	float33 invInertia;
	float invMass;
	int count;
};

struct Meteor
{
	int classID;
	RigidBody body;
};

class Game : public IGame
{
public:
	Game( HINSTANCE hInst, HWND hWnd );
	~Game();

	virtual void onCreateGraphics( D3DDevice device );
	virtual void onResetGraphics( D3DDevice device );

	virtual void onFrameMove(float dt);
	virtual void onFrameRender(float dt);

	virtual void onLostGraphics();
	virtual void onDestroyGraphics();

	// user-defined functions
	void createMeteorClasses();

	void handleInput();

	void meteorManagement( float dt );

	void renderMeteors();

private:
	D3DDevice	m_d3dDevice;

	SInput		m_input;
	Camera		m_camera;

	poly::Polygon	m_polygon;
	PolygonView	m_polygonView;

	// game-specific
	Player m_player;

	Pool<MeteorClass*> m_metClasses;
	Pool<Meteor> m_meteors;
	float m_meteorBuilder;
};
